<?php

namespace App\Models;

use App\Traits\HasDateTimeFormatter;
use App\Traits\Ownable;
use App\Traits\RangeSearch;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LoanApplication extends Model
{
    use HasFactory, HasDateTimeFormatter, Ownable, RangeSearch;

    protected $table = "loan_application";

    protected $appends = [
        'amount_start_zh',
        'amount_end_zh',
        'category_name',
        'status_name',
        'source_name'
    ];

    protected $casts =[
        'user_id' => 'integer',
        'status' => 'integer',
        'price_money' => 'float',
        'price_ticket' => 'integer',
        'is_allocated' => 'integer',
        'new_appid' => 'integer',
        'source' => 'integer',
        'amount_start' => 'integer',
        'amount_end' => 'integer',
        'need_home_service' => 'integer',
    ];

    /**
     * 操作流水
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function logs()
    {
        return $this->hasMany(LoanApplicationLog::class, 'loan_id');
    }

    /**
     * 所属用户
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function tags()
    {
        return $this->belongsToMany(LoanTag::class, 'loan_application_tag', 'loan_id', 'tag_id');
    }

    /**
     * 所属机构
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function org() {
        return $this->hasOne(Org::class, 'org_code', 'org_code');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function creditProvident()
    {
        return $this->hasOne(LoanCreditProvident::class, 'loan_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function mortgageCar()
    {
        return $this->hasOne(LoanMortgageCar::class, 'loan_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function mortgageHouse()
    {
        return $this->hasOne(LoanMortgageHouse::class, 'loan_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function mortgageTax()
    {
        return $this->hasOne(LoanMortgageTax::class, 'loan_id');
    }

    /**
     * @return Attribute
     */
    protected function amountStartZh(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                return toZhNum($attributes['amount_start']);
            },
        );
    }

    /**
     * @return Attribute
     */
    protected function amountEndZh(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                return toZhNum($attributes['amount_end']);
            },
        );
    }


    /**
     * @return Attribute
     */
    protected function categoryName(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                $define = [
                    'general' => '银行综合贷款',
                    'credit-provident' => '公积金信用贷',
                    'mortgage-car' => '车辆抵押贷',
                    'mortgage-house' => '房屋抵押贷',
                    'mortgage-tax' => '企业税贷',
                ];
                return $define[$attributes['category']] ?? "-";
            },
        );
    }

    /**
     * @return Attribute
     */
    protected function statusName(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                $define = [
                    1 => '待跟进',
                    '已通话',
                    '已上门',
                    '已签约',
                    '已放款',
                    '已拒绝',
                    '恶意/骚扰',
                    '无效对话',
                    '空错号',
                    '回访-电话3次未接通',
                    '非本人',
                    '无意向',
                    '异地',
                    '资质不符',
                    '退款中',
                    '退款成功',
                    '退款失败',
                ];
                return $define[$attributes['status']] ?? "";
            },
        );
    }

    /**
     * @return Attribute
     */
    protected function sourceName(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                $define = [
                    0 => '未知',
                    1 => '导入',
                ];
                return $define[$attributes['source']] ?? "-";
            },
        );
    }


}
